<?php

class Admin extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model("type_model");
        $this->load->model("blog_model");
        $this->load->model("widget_model");
        $this->load->model("text_model");
        $this->load->model("textlink_model");
        $this->load->model("image_model");
        $this->load->library('parser');
    }

    private function init()
    {
        $data['base_url'] = $this->config->item('base_url');
        $data["blogs"] = $this->blog_model->get_blogs();
        $data["texts"] = $this->text_model->get_texts();
        $data["textlinks"] = $this->textlink_model->get_textlinks();
        $data['error'] = '';
        return $data;
    }

    public function index()
    {
        $data = $this->init();
		$this->parser->parse('admin_index',$data);
    }

    public function add_blog()
    {
        $data['base_url'] = $this->config->item('base_url');
        $data["types"] = $this->type_model->get_types();
        $this->parser->parse('admin_add_blog',$data);
    }

    public function create_blog()
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('content', 'Content', 'required');
        $this->form_validation->set_rules('widget', 'widget', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $data = $this->init();
            $data['error'] = '操作失败：带*的字段不能为空。请重新操作！';
            $this->parser->parse('admin_index',$data);
        }
        else 
        {
            if($this->widget_model->get_widget_byname($this->input->post('widget')))
            {
                $data = $this->init();
                $data['error'] = '操作失败：widgets名称重复。请重新操作！';
                $this->parser->parse('admin_index',$data);
            }
            else
            {
                $this->widget_model->set_widget('blog');
                $this->blog_model->set_blog();
                header('Location: '.$this->config->item('base_url').'/index.php/admin');
                //$this->load->view('admin_index');
            }
        }

    }

    public function delete_blog($id)
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->blog_model->delete_blog($id);
        header('Location: '.$this->config->item('base_url').'/index.php/admin');
    }

    public function edit_blog($id)
    {
        $data['base_url'] = $this->config->item('base_url');
        $data['blog'] = $this->blog_model->get_blog_byid($id);
		$this->parser->parse('admin_edit_blog',$data);
    }

    public function update_blog()
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('content', 'Content', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $data = $this->init();
            $data['error'] = '操作失败：带*的字段不能为空。请重新操作！';
            $this->parser->parse('admin_index',$data);
        }
        else 
        {
            $this->blog_model->update_blog();
            header('Location: '.$this->config->item('base_url').'/index.php/admin');
        }

    }

    public function add_text()
    {
        $data['base_url'] = $this->config->item('base_url');
        $data["types"] = $this->type_model->get_types();
        $this->parser->parse('admin_add_text',$data);
    }

    public function create_text()
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('textvalue', 'Content', 'required');
        $this->form_validation->set_rules('widget', 'widget', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $data = $this->init();
            $data['error'] = '操作失败：带*的字段不能为空。请重新操作！';
            $this->parser->parse('admin_index',$data);
        }
        else 
        {
            if($this->widget_model->get_widget_byname($this->input->post('widget')))
            {
                $data = $this->init();
                $data['error'] = '操作失败：widgets名称重复。请重新操作！';
                $this->parser->parse('admin_index',$data);
            }
            else
            {
                $this->widget_model->set_widget('text');
                $this->text_model->set_text();
                header('Location: '.$this->config->item('base_url').'/index.php/admin');
                //$this->load->view('admin_index');
            }
        }

    }

    public function delete_text($id)
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->text_model->delete_text($id);
        header('Location: '.$this->config->item('base_url').'/index.php/admin');
    }

    public function edit_text($id)
    {
        $data['base_url'] = $this->config->item('base_url');
        $data['text'] = $this->text_model->get_text_byid($id);
		$this->parser->parse('admin_edit_text',$data);
    }

    public function update_text()
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('textvalue', 'textvalue', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $data = $this->init();
            $data['error'] = '操作失败：带*字段不能为空。请重新操作！';
            $this->parser->parse('admin_index',$data);
        }
        else 
        {
            $this->text_model->update_text();
            header('Location: '.$this->config->item('base_url').'/index.php/admin');
        }

    }

    public function add_textlink()
    {
        $data['base_url'] = $this->config->item('base_url');
        $data["types"] = $this->type_model->get_types();
        $this->parser->parse('admin_add_textlink',$data);
    }

    public function create_textlink()
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('url', 'URL', 'required');
        $this->form_validation->set_rules('textvalue', 'textvalue', 'required');
        $this->form_validation->set_rules('widget', 'widget', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $data = $this->init();
            $data['error'] = '操作失败：带*字段不能为空。请重新操作！';
            $this->parser->parse('admin_index',$data);
        }
        else 
        {
            if($this->widget_model->get_widget_byname($this->input->post('widget')))
            {
                $data = $this->init();
                $data['error'] = '操作失败：widgets名称重复。请重新操作！';
                $this->parser->parse('admin_index',$data);
            }
            else
            {
                $this->widget_model->set_widget('textlink');
                $this->textlink_model->set_textlink();
                header('Location: '.$this->config->item('base_url').'/index.php/admin');
                //$this->load->view('admin_index');
            }
        }

    }

    public function delete_textlink($id)
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->textlink_model->delete_textlink($id);
        header('Location: '.$this->config->item('base_url').'/index.php/admin');
    }

    public function edit_textlink($id)
    {
        $data['base_url'] = $this->config->item('base_url');
        $data['textlink'] = $this->textlink_model->get_textlink_byid($id);
		$this->parser->parse('admin_edit_textlink',$data);
    }

    public function update_textlink()
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('textvalue', 'textvalue', 'required');
        $this->form_validation->set_rules('url', 'URL', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $data = $this->init();
            $data['error'] = '操作失败：带*字段不能为空。请重新操作！';
            $this->parser->parse('admin_index',$data);
        }
        else 
        {
            $this->textlink_model->update_textlink();
            header('Location: '.$this->config->item('base_url').'/index.php/admin');
        }

    }

    public function add_image()
    {
        $data['base_url'] = $this->config->item('base_url');
        $data["types"] = $this->type_model->get_types();
        $data["error"] = "";
        $this->load->helper(array('form', 'url'));
        $this->parser->parse('admin_add_image',$data);
    }

    public function create_image()
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('widget', 'widget', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $data = $this->init();
            $data['error'] = '操作失败：带*字段不能为空。请重新操作！';
            $this->parser->parse('admin_index',$data);
        }
        else 
        {
            if($this->widget_model->get_widget_byname($this->input->post('widget')))
            {
                $data = $this->init();
                $data['error'] = '操作失败：widgets名称重复。请重新操作！';
                $this->parser->parse('admin_index',$data);
            }
            else
            {
                $config['upload_path'] = './upload/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['encrypt_name'] = true;
                $this->load->library('upload', $config);


                if (!$this->upload->do_upload('image'))
                {
                    $error = array('error' => $this->upload->display_errors());
                    $data = $this->init();
                    $data['error'] = $error['error'];
                    $this->parser->parse('admin_index',$data);
                } 
                else
                {
                    $upload_data = $this->upload->data();
                    //print_R($upload_data);exit;

                    $data = $this->init();
                    $data['upload_data'] = $upload_data;
                    $this->widget_model->set_widget('image');
                    $this->image_model->set_image($upload_data);
                    header('Location: '.$this->config->item('base_url').'/index.php/admin');
                }
            }
        }

    }

    public function delete_image($id)
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->textlink_model->delete_textlink($id);
        header('Location: '.$this->config->item('base_url').'/index.php/admin');
    }

    public function delete_widget($widget)
    {
        $data['base_url'] = $this->config->item('base_url');
        $this->widget_model->delete_widget($widget);
        header('Location: '.$this->config->item('base_url').'/index.php/admin');
    }
}
?>
